package com.application.Activity;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Network.Protocol;

import ReadWriteXML.CMember;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class NetworkStateReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.d(Protocol.TAG,"NetworkStateReceiver"+ action);
		if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
            return;
        }

        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 
        NetworkInfo aNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);                     

        if (!noConnectivity)
        {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) ||
                (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
            {
                //Handle connected case
            	Toast.makeText(context, "Connect", 1).show();
            	Log.d(Protocol.TAG,"NetworkStateReceiver Handle connected case ");
            }
        }
        else
        {
            if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) ||
                    (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
            {
                //Handle disconnected case
            	if(NetworkController.isOutDoor() && CommonUtilities.nguoiDung!=null){
            		CommonUtilities.isExit=true;
            		
            	Toast toast=Toast.makeText(context, "Mất kết nối Internet", Toast.LENGTH_LONG);
            	LinearLayout llView=new LinearLayout(context);
            	llView.setOrientation(LinearLayout.HORIZONTAL);
            	ImageView img=new ImageView(context);
            	img.setBackgroundDrawable(Drawable.createFromPath("ic_disconnect.png"));
            	TextView txtNoiDung=new TextView(context);
            	txtNoiDung.setText("Mất kết nối Internet");
            	llView.addView(img);
            	llView.addView(txtNoiDung);
            	toast.setView(llView);
            	toast.show();
            		
            		Log.d(Protocol.TAG,"NetworkStateReceiver Handle connected case ");
            		ActivityController.finishAllActivity();
            	}
            	
            }
        }

		
	}

}
